package com.ryan.mms.organization.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ryan.dictionary.framework.enums.OrganizationLevelEnum;
import com.ryan.dictionary.framework.enums.OrganizationTypeEnum;
import com.ryan.framework.action.BaseAction;
import com.ryan.framework.tag.pagination.Page;
import com.ryan.mms.organization.dto.OrganizationDto;
import com.ryan.mms.organization.model.Organization;
import com.ryan.mms.organization.service.OrganizationService;
import com.ryan.utils.EnumUtil;

@Component
public class OrganizationAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private OrganizationDto organizationDto;
	private Page page;
	@Autowired
	private OrganizationService<OrganizationDto,Organization,Long> organizationService;
	private String selectedOrganization;


	public String list() throws Exception {
		if(new Integer(-1).equals(organizationDto.getOrganizationLevel())){
			organizationDto.setOrganizationLevel(null);
		}
		if(new Integer(-1).equals(organizationDto.getOrganizationType())){
			organizationDto.setOrganizationType(null);
		}
		List<OrganizationDto> organizationList = organizationService.find(page, organizationDto);
		this.getActionContext().put("page", page);
		this.getActionContext().put("organizationList", organizationList);
		this.getActionContext().put("organizationLevelList", EnumUtil.getEnumList(OrganizationLevelEnum.class));
		this.getActionContext().put("organizationTypeList", EnumUtil.getEnumList(OrganizationTypeEnum.class));
		this.getActionContext().put("organizationDto", organizationDto);
		return SUCCESS;
	}
	
	public String index() throws Exception {
		page = new Page();
		organizationDto = new OrganizationDto();
		List<OrganizationDto> organizationList = organizationService.find(page, organizationDto);
		this.getActionContext().put("page", page);
		this.getActionContext().put("organizationList", organizationList);
		this.getActionContext().put("organizationLevelList", EnumUtil.getEnumList(OrganizationLevelEnum.class));
		this.getActionContext().put("organizationTypeList", EnumUtil.getEnumList(OrganizationTypeEnum.class));
		this.getActionContext().put("organizationDto", organizationDto);
		return SUCCESS;
	}
	
	public String select() throws Exception {
		if(null==page){
			page = new Page();
		}
		if(null==organizationDto){
			organizationDto = new OrganizationDto();
		}
		if(null==selectedOrganization){
			selectedOrganization="";
		}
		if(new Integer(-1).equals(organizationDto.getOrganizationLevel())){
			organizationDto.setOrganizationLevel(null);
		}
		if(new Integer(-1).equals(organizationDto.getOrganizationType())){
			organizationDto.setOrganizationType(null);
		}
		List<OrganizationDto> organizationList = organizationService.find(page, organizationDto);
		this.getActionContext().put("page", page);
		this.getActionContext().put("organizationList", organizationList);
		this.getActionContext().put("organizationLevelList", EnumUtil.getEnumList(OrganizationLevelEnum.class));
		this.getActionContext().put("organizationTypeList", EnumUtil.getEnumList(OrganizationTypeEnum.class));
		this.getActionContext().put("organizationDto", organizationDto);
		selectedOrganization = selectedOrganization.replaceAll(" ", "");
		this.getActionContext().put("selectedOrganization", selectedOrganization);
		return SUCCESS;
	}

	public OrganizationDto getOrganizationDto() {
		return organizationDto;
	}

	public void setOrganizationDto(OrganizationDto organizationDto) {
		this.organizationDto = organizationDto;
	}

	public void setOrganizationService(
			OrganizationService<OrganizationDto, Organization, Long> organizationService) {
		this.organizationService = organizationService;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public Page getPage() {
		return page;
	}

	public String getSelectedOrganization() {
		return selectedOrganization;
	}

	public void setSelectedOrganization(String selectedOrganization) {
		this.selectedOrganization = selectedOrganization;
	}

}
